//
//  EzText.cpp
//  EzTools
//
//  Created by 刘跃龙 on 11/9/15.
//  Copyright © 2015 Frank He. All rights reserved.
//

#include "EzText.h"
#include <time.h>
#include <iostream>
#include "ABMUtils.h"

std::string EzText::genNumberPassword(int len){
    std::string ret;
    if(len < 1) len = 8;
    //srand((int)time(NULL));
    for(int i = 0 ; i < len ; i ++){
        int value = rand()%10 + 0x30;
        ret.append(1, value);
    }
    return ret;
};

std::string EzText::genCharacterAndNumberPassword(int len){
    std::string ret;
    if(len < 1) len = 8;
    //srand((int)time(NULL));
    for(int i = 0 ; i < len ; i ++){
        int value = rand()%61 + 0x30;
        if(value > 0x39) value += 7;
        if(value > 0x5A) value += 6;
        ret.append(1, value);
    }
    return ret;
};

std::string EzText::StringToHexString(std::string str)
{
    std::string output;
    char temp[8];
    for (int i = 0; i < str.length(); ++i)
    {
        sprintf(temp,"%.2x",*(uint8_t*)(str.data()+i));
        output.append(temp,2);
    }
    return output;
}

std::string EzText::HexStringtoString(std::string str)
{
    std::string output;
    const char* pString = str.c_str();
    for (int i = 0; i < str.size(); i++)
    {
        if ( (pString[i] >='0' && pString[i] <='9') || (pString[i] >='A' && pString[i] <='F') || (pString[i] >='a' && pString[i] <='f') ) //属于0-9集合，返回是
        {
            
        }
        else
        {
            return "";
        }
    }
    for (int i = 0; i < str.length(); i+=2)
    {
        std::string str_temp = str.substr(i,2);
        int temp = ::strtol(str_temp.c_str(),NULL,16);
        output.append(1,temp);
    }
    return output;
}

std::string EzText::trim(std::string str,std::string pattern/* = " "*/)
{
    while (1) {
        std::string::size_type pos = str.find(pattern,0);
        if(pos == 0){
            str = str.substr(pattern.size());
        }else{
            break;
        }
    }
    while (1) {
        std::string::size_type pos = str.find(pattern,str.size()-pattern.size());
        if(pos == str.size()-pattern.size()){
            str = str.substr(0,str.size()-pattern.size());
        }else{
            break;
        }
    }
    return str;
}

std::string EzText::strRemove(std::string str,char ch)
{
    std::string return_str="";
    int length=(int)(str.length());
    for(int i=0;i<length;i++)
        if(str[i]!=ch)return_str+=str[i];
    return return_str;
}

std::vector<std::string> EzText::split(std::string str, std::string pattern)
{
    std::string::size_type pos;
    std::vector<std::string> result;
    str += pattern;
    int size = (int)(str.size());
    
    for (int i = 0; i < size; i++) {
        pos = str.find(pattern, i);
        if (pos < size) {
            std::string s = str.substr(i, pos - i);
            result.push_back(s);
            i = (int)(pos + pattern.size() - 1);
        }
    }
    
    return result;
}

std::string EzText::uint16ToNetworkString(uint16_t value){
    std::string ret = "";
    value = htons(value);
    ret.append(1,(value & 0xFF));
    ret.append(1,((value >> 8) & 0xFF));
    return ret;
}

std::string EzText::uint32ToNetworkString(uint32_t value){
    std::string ret = "";
    value = htonl(value);
    ret.append(1,(value & 0xFF));
    ret.append(1,((value >> 8) & 0xFF));
    ret.append(1,((value >> 16) & 0xFF));
    ret.append(1,((value >> 24) & 0xFF));
    return ret;
}

uint32_t EzText::networkCharPointToUint32(char* buf,size_t offset)
{
    std::string temp = std::string(buf+offset,4);
    return networkStringPointToUint32(temp,0);
}

uint16_t EzText::networkCharPointToUint16(char* buf,size_t offset)
{
    std::string temp = std::string(buf+offset,2);
    return networkStringPointToUint16(temp,0);
}

uint16_t EzText::networkStringPointToUint16(std::string buf,size_t offset){
    std::string subStr = buf.substr(offset,4);
    uint16_t ret = *(uint16_t*)(subStr.data());
    ret = ntohs(ret);
    return ret;
}

uint32_t EzText::networkStringPointToUint32(std::string buf,size_t offset){
    std::string subStr = buf.substr(offset,4);
    uint32_t ret = *(uint32_t*)(subStr.data());
    ret = ntohl(ret);
    return ret;
}

EZ_BEGIN


long getTimestmap(long offset/* = 0*/)
{
    struct timeval now;
    gettimeofday(&now, NULL);
    time_t timep = now.tv_sec;
    return (timep + offset);
}

long long getMillTimestmap(long long offset/* = 0*/)
{
    struct timeval now;
    gettimeofday(&now, NULL);
    time_t timep = now.tv_sec;
    return (timep + offset)*1000+now.tv_usec/1000;
}

std::string getTimestmapString(){
    std::string format = "%F %T";
    long long millTimestmap = getMillTimestmap();
    return getTimestmapString(format,millTimestmap);
}
std::string getGMTTimestmapString()
{
    std::string format = "%F %T";
    long long millTimestmap = getMillTimestmap();
    return getGMTTimestmapString(format,millTimestmap);
}
std::string getTimestmapString(std::string format)
{
    long long millTimestmap = getMillTimestmap();
    return getTimestmapString(format,millTimestmap);
}
std::string getGMTTimestmapString(std::string format)
{
    long long millTimestmap = getMillTimestmap();
    return getGMTTimestmapString(format, millTimestmap);
}
std::string getTimestmapString(std::string format,long timestmap){
    long long millTimestmap = ((long long)timestmap)*1000;
    return getTimestmapString(format,millTimestmap);
}
std::string getGMTTimestmapString(std::string format,long timestmap)
{
    long long millTimestmap = ((long long)timestmap)*1000;
    return getGMTTimestmapString(format,millTimestmap);
}
std::string getTimestmapString(std::string format,long long millTimestmap)
{
    char buffer[80];
    sprintf(buffer,"%03d",(int)(millTimestmap%1000));
    std::size_t pos = format.find("%s");
    while(pos != std::string::npos){
        format.replace(pos,2,std::string(buffer));
        pos = format.find("%s",pos);
    }
    time_t timestmap = millTimestmap/1000;
    struct tm* tm = localtime(&timestmap);
    strftime (buffer,80,format.c_str(),tm);
    return std::string(buffer);
}
std::string getGMTTimestmapString(std::string format,long long millTimestmap)
{
    char buffer[80];
    sprintf(buffer,"%03d",(int)(millTimestmap%1000));
    std::size_t pos = format.find("%s");
    while(pos != std::string::npos){
        format.replace(pos,2,std::string(buffer));
        pos = format.find("%s",pos);
    }
    time_t timestmap = millTimestmap/1000;
    struct tm* tm = gmtime(&timestmap);
    strftime (buffer,80,format.c_str(),tm);
    return std::string(buffer);
}

EZ_END
